library(readr)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ dplyr   1.0.7
## ✓ tibble  3.1.4     ✓ stringr 1.4.0
## ✓ tidyr   1.1.3     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(mapview)
## Warning: multiple methods tables found for 'crop'
## Warning: multiple methods tables found for 'extend'
library(sf)
## Linking to GEOS 3.8.1, GDAL 3.2.1, PROJ 7.2.1
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(leaflet)
library(censusapi)
## 
## Attaching package: 'censusapi'
## The following object is masked from 'package:methods':
## 
##     getFunction
library(devtools)
## Loading required package: usethis
#claims_public <- read_csv(file="/Users/pacopoler/Desktop/s22-218z-fema/FimaNfipClaims.csv")
#claims_public <- read_csv("/Volumes/GoogleDrive/Shared drives/S22 218Z/FEMA/Data/Fema) Downloads/FimaNfipClaims.csv")
claims_public <- readRDS("claims.rds")
bay_county_names <-
  c(
    "Alameda",
    "Contra Costa",
    "Marin",
    "Napa",
    "San Francisco",
    "San Mateo",
    "Santa Clara",
    "Solano",
    "Sonoma"
  )

CA_counties <- counties("CA")
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |=========                                                             |  14%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |==========================================                            |  61%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |===================================================                   |  74%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |===============================================================       |  91%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
bay_counties <- filter(CA_counties, NAME %in% bay_county_names) %>%
  rename('countyCode' = 'GEOID') %>%
  select(countyCode, NAME) %>%
  mutate(NAME = toupper(NAME)) %>%
  st_drop_geometry()


bay_public <- filter(claims_public, countyCode %in% bay_counties$countyCode) %>%
  select(censusTract, countyCode, yearOfLoss, amountPaidOnBuildingClaim) %>%
  rename('year' = 'yearOfLoss') %>%
  group_by(censusTract, year) %>%
  summarize(
    total_claims = n(),
    total_claim_value = sum(amountPaidOnBuildingClaim, na.rm = T),
    capped_claims = min(n(), 200),
    countyCode = countyCode[which.max(countyCode)]
  ) %>%
  left_join(bay_counties) %>%
  mutate(
    # NAME = (toupper(NAME) %>% paste('COUNTY', sep=' ')),
    average_claim_value = total_claim_value / total_claims
  )
## `summarise()` has grouped output by 'censusTract'. You can override using the `.groups` argument.
## Joining, by = "countyCode"
# NOW IMPORT CENSUS DATA

Sys.setenv(CENSUS_KEY="c8aa67e4086b4b5ce3a8717f59faa9a28f611dab")

pop_2020 <- tibble()

for (county in c(
  "state:06+county:097", 
  "state:06+county:075", 
  "state:06+county:041",
  "state:06+county:055",
  "state:06+county:095",
  "state:06+county:013",
  "state:06+county:085",
  "state:06+county:001",
  "state:06+county:081"
)) {
  pop_2020 <- rbind(pop_2020, (getCensus(
    name = "dec/pl",
    vintage = 2010,
    region = "tract:*", 
    regionin = county,
    # vars = "P1_001N"
    vars = "P001001"
  ) %>% 
    transmute(
      GEOID =
        paste0(state,county,tract),
      pop = P001001
    )
  )
  )
}

# bay area census visualization

tracts_2020 <- tibble()

for (county in bay_counties$NAME){
  tracts_2020 <- rbind(
    tracts_2020, 
    tracts("CA", county, year = 2010, progress_bar = F)
  )
}

pop_2020 <- pop_2020 %>% 
  left_join(
    tracts_2020 %>% 
      select(GEOID10, geometry) %>% 
      rename('GEOID' = 'GEOID10')
    ) %>% 
  st_as_sf()
## Joining, by = "GEOID"
ggplot(pop_2020) + 
  geom_sf(aes(geometry=geometry, fill = pop))

pop_pal <- colorNumeric(
  palette = "Blues",
  domain = pop_2020$pop
)

leaflet() %>% 
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addPolygons(
    data = pop_2020,
    fillColor = ~pop_pal(pop),
    color = "white",
    weight = 0.5,
    fillOpacity = 0.5,
    label = ~pop
  ) %>%
  addLegend(
    position = "bottomright",
    pal = pop_pal,
    values = pop_2020$pop,
    title = "Bay Area Population - 2020"
  )
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
claims_aggregated <- group_by(bay_public, censusTract) %>%
  summarize(
    total_claims = sum(total_claims),
    total_claim_value = sum(total_claim_value)
  ) %>%
  left_join((pop_2020 %>% rename('censusTract' = 'GEOID'))) %>%
  mutate(
    claim_density = total_claims / pop,
    claim_value_density = total_claim_value / pop
  )%>% 
  st_as_sf()
## Joining, by = "censusTract"
ggplot(claims_aggregated %>% filter(total_claims < 400)) + 
  geom_sf(aes(geometry=geometry, fill = total_claims), color=NA)

#map<-claims_aggregated %>% filter(total_claims < 400)
agg_pal <- colorNumeric(
  palette = "plasma",
  domain = claims_aggregated$total_claims
)

leaflet() %>% 
  addProviderTiles(providers$CartoDB.Positron) %>% 
  addPolygons(
    data = claims_aggregated,
    fillColor = ~agg_pal(total_claims),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~total_claims
  ) %>%
  addLegend(
    position = "bottomright",
    pal = agg_pal,
    values = claims_aggregated$total_claims,
    title = "Aggregated Claims 2020"
  )
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD83 +no_defs).
## Need '+proj=longlat +datum=WGS84'
# POLIICES
policies_bay <- read_csv("/Volumes/GoogleDrive/Shared drives/S22 218Z/FEMA/Data/Fema) Downloads/fema_policies_bay.rds")
#policies_bay <- readRDS("/Users/pacopoler/Desktop/s22-218z-fema/fema_policies_bay.rds")
policies_by_tract <- group_by(policies_bay, censusTract) %>%
  summarize(
    policy_count = n()
  )

claims_aggregated <- claims_aggregated %>%
  left_join(policies_by_tract) %>%
  mutate(
    log_claims_per_policy = log(total_claims / policy_count),
    claims_per_policy = total_claims / policy_count,
    policy_coverage_pct = policy_count / pop * 100
  )

ggplot(claims_aggregated) + 
  geom_sf(aes(geometry=geometry, fill = policy_coverage_pct), color=NA)


# OUTCOME 1: claims by county by year

claims_by_year <- group_by(bay_public, censusTract, year) %>%
  summarize(
    total_claims = sum(total_claims),
    total_claim_value = sum(total_claim_value)
  ) %>%
  left_join(pop_2020) %>%
  mutate(
    claim_density = total_claims / pop,
    claim_value_density = total_claim_value / pop
  )

ggplot(claims_by_year) +
  geom_point(aes(x=year, y=claim_density)) +
  ylab('total number of claims per capita')

ggplot(county_data) +
  geom_histogram(aes(x=average_claim_value), bins=60) +
  ggtitle(county) +
  ylab('number of census tracts') +
  xlab('average claim value')

# OUTCOME 2: policy coverage

years <- paste('01/01', (2010:2021), sep='/') %>%
  as.Date("%m/%d/%Y")

policy_coverage_by_year <- group_by(policies_bay, 'censusTract') %>%

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.